package org.cx.web;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.cx.mode.UserDetail;

/**
 * Servlet implementation class UserDetailQueryById
 */
public class UserDetailQueryById extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserDetailQueryById() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		
		String un=(String) request.getSession().getAttribute("userName");
		if(un==null || "".equals(un)){
			//response.getWriter().print("请先登录");
			request.getRequestDispatcher("cs_login/login.jsp").forward(request, response);
			return;
		}
		
		String id = request.getParameter("id");
		Connection conn= null;
		PreparedStatement pstmt = null;
		UserDetail us = new UserDetail();
		
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/cs?useUnicode=true&characterEncoding=utf-8","root","123123");
			String sql = "SELECT * FROM user_detail where id=?";
			pstmt=conn.prepareStatement(sql);
			pstmt.setInt(1, Integer.parseInt(id));
			ResultSet rs = pstmt.executeQuery();
			while(rs.next()){
				us.setId(rs.getInt("id"));
				us.setNickName(rs.getString("nick_name"));
				us.setEmail(rs.getString("email"));
				us.setPasswd(rs.getString("passwd"));
				us.setIntro(rs.getString("intro"));
				us.setSex(rs.getInt("sex"));
				
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				pstmt.close();
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		request.setAttribute("us", us);
		request.getRequestDispatcher("cs_login/user_detail.jsp").forward(request, response);
	}

}
